﻿using System;
using System.Web.UI;

namespace Hanhud.Web.UI
{
    public class BaseUserControl : UserControl
    {
        public string GetImageUrl(string imageUrl)
        {
            return GetImageUrl(imageUrl, false);
        }

        public string GetImageUrl(string imageUrl, bool useTheme)
        {
            if (useTheme)
            {
                if (imageUrl.IndexOf("~/") >= 0) imageUrl = imageUrl.Replace("~/", "");
                else if (imageUrl[0] == '/') imageUrl = imageUrl.Remove(0);

                return
                    Page.ResolveUrl(string.Format("~/App_Themes/{0}/{1}", Page.Theme, imageUrl));
            }
            else
                return Page.ResolveUrl(imageUrl);
        }

        public string GetPageUrl(string pageUrl)
        {
            string strBaseUrl = string.Empty; // CUrlBuilder.Path;

            if (pageUrl.IndexOf("~/") >= 0) pageUrl = pageUrl.Replace("~/", "");
            else if (pageUrl[0] == '/') pageUrl = pageUrl.Remove(0);

            //return string.Format("http://localhost{0}{1}", strBaseUrl, pageUrl);
            return string.Format("http://{2}{0}/{1}", Request.ApplicationPath, pageUrl, Request.Url.Host);
        }

        protected void RegisterCss(string href)
        {
            System.Web.UI.HtmlControls.HtmlLink gridCss = new System.Web.UI.HtmlControls.HtmlLink();
            gridCss.Attributes["type"] = "text/css";
            gridCss.Attributes["rel"] = "Stylesheet";

            if (href.IndexOf("~/") >= 0) href = href.Replace("~/", "");
            else if (href[0] == '/') href = href.Remove(0);

            gridCss.Href = string.Format("~/App_Themes/{0}/{1}", Config.CSiteConfig.Current.Theme, href);
            Page.Header.Controls.Add(gridCss);
        }

        protected void RegisterCss(string href, bool useTheme)
        {
            if (useTheme) RegisterCss(href);
            else
            {
                System.Web.UI.HtmlControls.HtmlLink gridCss = new System.Web.UI.HtmlControls.HtmlLink();
                gridCss.Attributes["type"] = "text/css";
                gridCss.Attributes["rel"] = "Stylesheet";

                if (href.IndexOf("~/") >= 0) href = href.Replace("~/", "");
                else if (href[0] == '/') href = href.Remove(0);

                gridCss.Href = string.Format("~/{0}", href);
                Page.Header.Controls.Add(gridCss);
            }
        }
    }
}
